package cn.smbms.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String FORMAT_DATE = "yyyy-MM-dd";
	public static String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static String FORMAT_DATESECOND = "yyyyMMddHHmmss";
	public static String FORMAT_MILLISECOND = "yyyyMMddHHmmssSSS";
	
	/**
	 * 比较时间，t1是否早于t2，不能等于 日期格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static boolean compareTo(String t1, String t2) {
		int i = 0;
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATETIME);
		try {
			Date time1 = sdf.parse(t1);
			Date time2 = sdf.parse(t2);
			i = time1.compareTo(time2);
		} catch (Exception e) {
			i = 0;
		}
		if (i < 0) {
			return true;
		} else {// 包括t1等于t2
			return false;
		}
	}

	/**
	 * 格式时间 格式时间值为空或者转化错误，则返回当前时间的格式
	 * 
	 * @param date
	 *            格式时间
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static String formatDate(String format) {
		return formatDate(new Date(), format);
	}

	public static String formatDate(String date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date tmpDate = null;
		try {
			tmpDate = df.parse(date);
		} catch (Exception e) {
			// 日期型字符串格式错误
			tmpDate = new Date();
		}
		return df.format(tmpDate);
	}

	/**
	 * 计算两个日期相隔的天数
	 * 
	 * @param dt1
	 * @param dt2
	 * @return
	 */
	public static int daysBetween(Date dt1, Date dt2) {
		int days = (int) (hoursBetween(dt1, dt2) / 24);
		return days;
	}

	public static int hoursBetween(Date dt1, Date dt2) {
		int hours = (int) (minutesBetween(dt1, dt2) / 60);
		return hours;
	}

	public static int minutesBetween(Date dt1, Date dt2) {
		int minutes = (int) (secondsBetween(dt1, dt2) / 60);
		return minutes;
	}

	public static int secondsBetween(Date dt1, Date dt2) {
		int secondsBetween = (int) ((dt2.getTime() - dt1.getTime()) / 1000);
		return secondsBetween;
	}

	// ///////////////////////////////////////////

	public static int daysBetween(String dt1, String dt2) {
		int days = (int) (hoursBetween(dt1, dt2) / 24);
		return days;
	}

	public static int hoursBetween(String dt1, String dt2) {
		int hours = (int) (minutesBetween(dt1, dt2) / 60);
		return hours;
	}

	public static int minutesBetween(String dt1, String dt2) {
		int minutes = (int) (secondsBetween(dt1, dt2) / 60);
		return minutes;
	}

	public static int secondsBetween(String dt1, String dt2) {
		SimpleDateFormat df = new SimpleDateFormat(FORMAT_DATETIME);
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = df.parse(dt1);
			d2 = df.parse(dt2);
			return secondsBetween(d1, d2);
		} catch (Exception e) {
			// 日期型字符串格式错误
			return 0;
		}
	}

	/**
	 * 格式化日期yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		return formatDate(date, FORMAT_DATETIME);
	}

	/**
	 * 格式化日期，格式出错返回当前时间
	 * 
	 * @param str
	 * @return
	 */
	public static Date stringToDate(String str) {
		if(str==null || "".equals(str)) return null;
		//
		DateFormat df = DateFormat.getDateInstance();
		Date date = null;
		try {
			date = df.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 计算日期
	 * 
	 * @param date
	 * @param day
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static String calDateTime(String date, int day, int hour,
			int minute, int second) {
		String result = "";
		SimpleDateFormat sd = new SimpleDateFormat(FORMAT_DATETIME);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(date));
			cal.add(Calendar.DATE, day);
			cal.add(Calendar.HOUR, hour);
			cal.add(Calendar.MINUTE, minute);
			cal.add(Calendar.SECOND, second);
			result = sd.format(cal.getTime());
		} catch (Exception e) {
			result = date;
		}
		return result;
	}

	public static String calDateTime(int day, int hour, int minute, int second) {
		return calDateTime(getDateTime(), day, hour, minute, second);
	}

	public static String calDateTime(int hour, int minute, int second) {
		return calDateTime(getDateTime(), 0, hour, minute, second);
	}

	public static String calDateTime(int minute, int second) {
		return calDateTime(getDateTime(), 0, 0, minute, second);
	}

	public static String calDateTime(int second) {
		return calDateTime(getDateTime(), 0, 0, 0, second);
	}

	/**
	 * 日期计算
	 * 
	 * @param date
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static String calDate(String date, int year, int month, int day) {
		String result = "";
		SimpleDateFormat sd = new SimpleDateFormat(FORMAT_DATE);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(date));
			cal.add(Calendar.MONTH, month);
			cal.add(Calendar.YEAR, year);
			cal.add(Calendar.DATE, day);
			result = sd.format(cal.getTime());
		} catch (Exception e) {
			result = date;
		}
		return result;
	}

	public static String calDate(int year, int month, int day) {
		return calDate(getDateTime(), year, month, day);
	}

	public static String calDate(int month, int day) {
		return calDate(getDateTime(), 0, month, day);
	}

	public static String calDate(int day) {
		return calDate(getDateTime(), 0, 0, day);
	}

	/**
	 * 当前时间，格式'yyyy-mm-dd HH:mm:ss'
	 * 
	 * @return
	 */
	public static String getDateTime() {
		return formatDate(FORMAT_DATETIME);
	}

	/**
	 * 当前日期，格式'yyyy-mm-dd'
	 * 
	 * @return
	 */
	public static String getDate() {
		return formatDate(FORMAT_DATETIME).substring(0, 10);
	}
	
	/**
	 * 当前时间，格式'HH:mm:ss'
	 * 
	 * @return
	 */
	public static String getTime() {
		return formatDate(FORMAT_DATETIME).substring(11);
	}
	
	/**
	 * 当前时间，格式'yyyyMMddHHmmss'
	 * 
	 * @return
	 */
	public static String getSimpleDateTime() {
		return formatDate(FORMAT_DATESECOND);
	}

	/**
	 * 当前时间，格式'yyyyMMddHHmmssSSS'
	 * 
	 * @return
	 */
	public static String getComplexDateTime() {
		return formatDate(FORMAT_MILLISECOND);
	}

	/**
	 * 取得System.currentTimeMillis()的值
	 * 
	 * @return long型
	 */
	public static long getMillisTime() {
		return System.currentTimeMillis();
	}

	/**
	 * 格式：1980年7月31日
	 * 
	 * @param date
	 *            格式'yyyy-mm-dd'
	 * @return
	 */
	public static String parseCnDate(String date) {
		String y = date.substring(0, 4);
		String m = delFrontZero(date.substring(5, 7));
		String d = delFrontZero(date.substring(8, 10));
		return y + "年" + m + "月" + d + "日";
	}

	/**
	 * 格式：1980.7.31
	 * 
	 * @param date
	 * @return
	 */
	public static String parseDotDate(String date) {
		String y = date.substring(0, 4);
		String m = delFrontZero(date.substring(5, 7));
		String d = delFrontZero(date.substring(8, 10));
		return y + "." + m + "." + d;
	}

	/**
	 * 除去日期字串前面的0
	 * 
	 * @param mord
	 * @return
	 */
	private static String delFrontZero(String mord) {
		int i = 0;
		try {
			i = Integer.parseInt(mord);
			return String.valueOf(i);
		} catch (Exception e) {
			return mord;
		}
	}

	/**
	 * 私有构造函数
	 * 
	 */
	private DateUtil() {
	}

}
